<template>
  <div class="app-container">
    <!--查询条件表单-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年级编号" prop="gradeCode">
        <el-input v-model="queryParams.gradeCode" placeholder="请输入年级编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="年级编码" prop="gradeEng">
        <el-input v-model="queryParams.gradeEng" placeholder="请输入年级编码" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="年级名称" prop="gradeName">
        <el-input v-model="queryParams.gradeName" placeholder="请输入年级名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="年级状态" clearable>
          <el-option v-for="dict in sys_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--按钮操作区-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermission="['school:grade:add']"
        >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermission="['school:grade:edit']"
        >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermission="['school:grade:remove']"
        >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermission="['school:grade:export']"
        >导出</el-button
        >
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--数据表格区-->
    <el-table v-loading="loading" :data="gradeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年级ID" align="center" prop="gradeId" width="70" />
<!--      <el-table-column label="年级编号" align="center" prop="gradeCode" />-->
<!--      <el-table-column label="年级编码" align="center" prop="gradeEng" />-->
      <el-table-column label="年级名称" align="center" prop="gradeName" width="90" />
      <el-table-column label="班级列表" align="center" prop="gradeClazzNameStr"/>
      <el-table-column label="班级数量" align="center" prop="gradeClazzCount" width="80"/>
      <el-table-column label="课程列表" align="center" prop="gradeCourseNameStr"/>
      <el-table-column label="课程数量" align="center" prop="gradeCourseCount" width="80"/>
      <el-table-column label="年级主任" align="center" prop="manageName" width="90" />
      <el-table-column label="年级排序" align="center" prop="gradeSort" width="80" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template #default="scope">
          <dict-tag :options="sys_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="206" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermission="['school:grade:edit']"
          >修改</el-button
          >
          <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermission="['school:grade:remove']"
          >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--表格分页-->
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改年级对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="gradeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年级编号" prop="gradeCode">
          <el-input-number v-model="form.gradeCode" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="年级名称" prop="gradeName">
          <el-input v-model="form.gradeName" placeholder="请输入年级名称" />
        </el-form-item>
        <el-form-item label="年级编码" prop="gradeEng">
          <el-input v-model="form.gradeEng" placeholder="请输入年级编码" />
        </el-form-item>
        <el-form-item label="年级顺序" prop="gradeSort">
          <el-input-number v-model="form.gradeSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="年级主任" prop="manageId">
          <el-select v-model="form.manageId" placeholder="请选择">
            <el-option
                v-for="item in teacherOptions"
                :key="item.teacherId"
                :label="item.teacherName"
                :value="item.teacherId"
                :disabled="item.status == 0"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Grade">
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import * as gradeApi from "@/api/school/gradeApi";
import * as publicApi from "@/api/school/publicApi";

/** 公共变量 */
const { proxy } = getCurrentInstance();
const { sys_status } = proxy.useDict('sys_status');
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    gradeCode: undefined,
    gradeEng: undefined,
    gradeName: undefined,
    status: undefined,
  },
  form: {
    gradeCode: undefined,
    gradeEng: undefined,
    gradeName: undefined,
    gradeSort: 0,
    manageId: undefined,
    status: 1,
    remark: undefined
  },
  rules: {
    manageId: [{ required: true, message: '年级主任不能为空', trigger: 'blur' }],
    gradeCode: [{ required: true, message: '年级编号不能为空', trigger: 'blur' }],
    gradeEng: [{ required: true, message: '年级编码不能为空', trigger: 'blur' }],
    gradeName: [{ required: true, message: '年级名称不能为空', trigger: 'blur' }],
    gradeSort: [{ required: true, message: '年级顺序不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
  }
})

const { queryParams, form, rules } = toRefs(data)


/** 查询表单相关变量 */
const showSearch = ref(true);


/** 表格相关变量*/
const loading = ref(true);
const gradeList = ref([]);
const total = ref(0);

/** 按钮操作区相关变量 */
const single = ref(true);
const multiple = ref(true);
const open = ref(false);
const title = ref('');
const ids = ref([]);
const teacherOptions = ref([]);

/** 查询年级列表 */
function getList() {
  loading.value = true;
  gradeApi
      .listGrade(queryParams.value)
      .then((response) => {
        gradeList.value = response.rows;
        total.value = response.total;
      })
      .finally(() => {
        loading.value = false;
      });
}
getList();


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef');
  handleQuery();
}

/** 表单重置 */
function reset() {
  form.value = {
    gradeId: undefined,
    gradeCode: undefined,
    gradeEng: undefined,
    gradeName: undefined,
    gradeSort: 0,
    manageId: undefined,
    status: 1,
    remark: undefined,
  };
  proxy.resetForm('gradeRef');
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = '添加年级';
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const gradeId = row.gradeId || ids.value;
  gradeApi.getGrade(gradeId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = '修改年级';
  });
}

publicApi.allTeacherList().then((response) => {
  teacherOptions.value = response
})

/** 删除按钮操作 */
function handleDelete(row) {
  const gradeIds = row.gradeId || ids.value;
  proxy.$modal
      .confirm(`是否确认删除年级ID为"${gradeIds}"的数据项？`)
      .then(() => gradeApi.deleteGrade(gradeIds))
      .then(() => {
        getList();
        proxy.$modal.msgSuccess('删除成功');
      })
      .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
      'school/grade/export',
      {
        ...queryParams.value,
      },
      `grade_${new Date().getTime()}.xlsx`,
  );
}


/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.gradeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs.gradeRef.validate((valid) => {
    if (valid) {
      if (form.value.gradeId != undefined) {
        gradeApi.updateGrade(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功');
          open.value = false;
          getList();
        });
      } else {
        gradeApi.addGrade(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功');
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

</script>

<style scoped>

</style>
